﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;

namespace Lesson19_练习题
{
    #region   接口的学习
    //人，汽车，房子，都需要登记，人到派出所，房子到房管所，车到车观所
    //使用接口实现登记方法 
    //不同事物之间的相同行为 

    interface IRegister
    { 
      void Register();

    }

    class Person : IRegister
    {
    public void Register()
        {
            Console.WriteLine("人到车管所登记");
        }
    }

    class Home : IRegister
    { 
    
    public void Register()
        {
            Console.WriteLine("房子到房管所登记");
        }
    }
    class Car : IRegister
    {
    public void Register()
        {
            Console.WriteLine("车到车管所登记");
        }
    }

    #region 显示接口成员实现
    interface ImyInterface1
    {
        int Add();

    }

    interface ImyInterface2

    {
       int Add();
    }


    class myClass : ImyInterface1, ImyInterface2
    {
        int ImyInterface1.Add()  //显示实现接口   接口名.行为名去实现它
        {
            int x = 3;
            int y = 4;

            return x + y;   
        } 
        int ImyInterface2.Add()
        {
            int x = 3;
            int y = 4;
            int z = 5;  

            return x + y + z;   
        }
    }
    class Computer
    {
        public ImyInterface1 id;
    }
    #endregion

    #endregion

    internal class Program
    {
        static void Main(string[] args)
        {
            #region
            IRegister[] arr = new IRegister[] { new Person() , new Home() ,new Car() };   
             for(int i = 0;i<arr.Length;i++)
            {
                arr[i].Register();   
            }
            #endregion
            #region   接口的学习
            ImyInterface1  myclass = new myClass();      //实例化接口继承对象
            ImyInterface1 imyInterface1 = myclass; //通过接口继承对象实例化接口 
            Console.WriteLine(imyInterface1.Add());
            Console.WriteLine( (myclass as ImyInterface2).Add());
            // 里氏替换原则
            ImyInterface2 p1 = new myClass();     //实例化接口继承类对象
            ImyInterface2 a = p1; 
            Console.WriteLine(a.Add()); 


            Computer s = new Computer();
            s.id = myclass;
            Console.WriteLine(s.id.Add() );
            
            #endregion 
        }
    }
}
